#include <iostream>
#include <set>
#include <string>
#include <utility>
using std::cout;
using std::endl;
using std::set;
using std::pair;
using std::string;


void test0(){
    set<int> nums;
    //nums2完成初始化后
    //已经去重并排序完毕
    set<int> nums2{1,2,3,2,10,1,0,5};
    /* set<int> nums3 = {1,2,3}; */

    set<int> nums3 = nums2;
    set<int> nums4(nums2.begin(),--nums2.end());

    for(auto & ele : nums4){
        cout << ele << " ";
    }
    cout << endl;
}

void test1(){
    //set的查找操作
    set<int> nums{1,2,3,4,5};
    int key = 5;
    size_t cnt = nums.count(key);
    cout << "count:" << cnt << endl;
    cnt = nums.count(10);
    cout << "count:" << cnt << endl;

    cout << endl;
    /* auto it = nums.find(4); */
    set<int>::iterator it = nums.find(40);
    if(it != nums.end()){
        cout << "该元素存在：" << *it << endl;
    }else{
        cout << "该元素不存在" << endl;
    }
}

//认识pair
void test2(){
    //一个pair对象理解为一个key-value（键值对）
    pair<int,string> pa(1,"hello");
    cout << pa.first << " " << pa.second << endl;

    pair<int,int> pa2(10,8);
    cout << pa2.first << " " << pa2.second << endl;
}

void test3(){
    //set的插入操作
    set<int> nums{1,2,3,4,5};
    /* auto ret = nums.insert(6); */
    pair<set<int>::iterator,bool> ret = nums.insert(50);
    if(ret.second){
        cout << "插入成功：" << *(ret.first) << endl;
    }else{
        cout << "插入失败，该元素已存在:" 
            << *(ret.first) << endl;
    }

    cout << endl;
    for(auto & ele : nums){
        cout << ele << " ";
    }
    cout << endl;

    int arr[5] = {18,41,35,2,99};
    //左闭右开
    nums.insert(arr,arr + 4);
    cout << endl;
    for(auto & ele : nums){
        cout << ele << " ";
    }
    cout << endl;

    set<int> nums2{100,44,33,22};
    nums.insert(nums2.begin(),nums2.end());
    cout << endl;
    for(auto & ele : nums){
        cout << ele << " ";
    }
    cout << endl;

    nums.insert({2,4,66,440});
    cout << endl;
    for(auto & ele : nums){
        cout << ele << " ";
    }
    cout << endl;
}

void test4(){
    set<int> nums{1,2,3,4,5};
    //set不支持下标访问
    /* nums[0]; */

    auto it = nums.begin();
    for(;it != nums.end();++it){
        cout << *it << " ";
    }
    cout << endl;

    --it;
    cout << *it << endl;
    --it;
    cout << *it << endl;
    /* *it = 400; */
}

int main(void){
    test4();
    return 0;
}
